<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/15
 * Time: 22:12
 */

namespace App\Library\form_builder\src\components;


class Check extends BaseForm
{

    protected $type='checkbox';

    const STATUS_CHECKBOX='status';
    protected $options='';

    /**
     * @param $type类型
     */
    public  function type($type){
        $this->type=$type;
        return $this;
    }

    /**
     * 生成组件
     *
     * @return array
     */
    public function build()
    {
        return [
            'type' => $this->type,
            'field' => $this->field,
            'title' => $this->title,
            'value' => $this->value,
            'options'=>$this->options
        ];
    }

//    <div class="layui-form-item newsTop">
//    <label class="layui-form-label"><i class="seraph icon-zhiding"></i> 置　顶</label>
//    <div class="layui-input-block">
//    <input type="checkbox" name="is_top" id="is_top" value="1" lay-skin="switch" lay-text="是|否">
//    </div>
//    </div>
    public function options($data){

        if(!is_array($data)){
            throw new \Exception('必须是数组');
        } else{
            $html='';
            switch ($this->type){
                case 'checkbox':
                    foreach ($data as $item=>$value){
                        $html.="<input type='{$this->type}' name='{$this->field}' value='{$value["value"]}' title='{$value["label"]}'>";
                    }break;
                case 'status':
                    if(count($data)!=2){
                        throw new \Exception('状态框只能传入长度为2的数组 既真与假两项');
                    }
                    $labels=array_column($data,"label");
                    if(empty($labels)){
                        throw new \Exception('键必须为label');
                    }
                    $label=implode('|',$labels);
                    $html.="<input type='checkbox' name='{$this->field}'  value='1' lay-skin='switch' lay-text={$label}>";
                  break;
                default:
                    throw new \Exception('没有该类型');
            }
            

            $this->options=$html;
        }
        return $this;
    }
}